package com.csjung.games.gamenori.client.ui;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.requestfactory.shared.Receiver;

import com.csjung.games.gamenori.client.event.OnMessageEvent;
import com.csjung.games.gamenori.shared.ChannelProxy;
import com.csjung.games.gamenori.shared.ChannelServiceRequest;
import com.csjung.games.gamenori.shared.GamenoriFactory;
import com.csjung.games.gamenori.shared.GamenoriRequestFactory;
import com.csjung.games.gamenori.shared.UserProxy;
import com.csjung.games.gamenori.shared.UserServiceRequest;

import java.util.List;

public class ChatDialogBox extends WindowDialogBox {

  private final GamenoriRequestFactory requestFactory;
 
  /**
   * The CellList.
   */
  private CellList<String> cellList;

  private ListDataProvider<String> dataProvider = new ListDataProvider<String>();

  private TextBox inputTxt = new TextBox();
  
  private GamenoriFactory factory = GWT.create(GamenoriFactory.class);
  
  private ChannelProxy channelProxy;

  public ChannelProxy getChannelProxy() {
    return channelProxy;
  }

  public void setChannelProxy(ChannelProxy channelProxy) {
    this.channelProxy = channelProxy;
  }

  /**
   * Creates a new empty window with default layout.
   * @param userProxy 
   */
  public ChatDialogBox(final GamenoriRequestFactory requestFactory) {    
    super();
    this.requestFactory = requestFactory;
    VerticalPanel panel = new VerticalPanel();
    cellList = new CellList<String>(new TextCell());
    cellList.setHeight("250px");
    cellList.setWidth("200px");
    inputTxt.setWidth("200px");
    
    // Add the cellList to the dataProvider.
    dataProvider.addDataDisplay(cellList);
    
    panel.add(cellList);
    panel.add(inputTxt);
    setWidget(panel);
    
    inputTxt.addKeyDownHandler(new KeyDownHandler() {
      public void onKeyDown(KeyDownEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
          sendMsg();
        }
      }
    });
    
    requestFactory.getEventBus().addHandler(OnMessageEvent.TYPE, new OnMessageEvent.Handler() {
      public void onMessage(OnMessageEvent event) {
        receiveMsg(event.getMessage());
      }
    });
  }
  
  public void createChannel(String contactUserId) {
    ChannelServiceRequest request = requestFactory.channelServiceRequest();
    request.requestChat(contactUserId).to(new Receiver<ChannelProxy>() {
      @Override
      public void onSuccess(ChannelProxy response) {
        channelProxy = response;
      }
    });
    request.fire();
  }
  
  public void contactChannel(ChannelProxy channel) {
    UserServiceRequest request = requestFactory.userServiceRequest();
    request.getUser(channel.getCreateUserId()).to(new Receiver<UserProxy>() {
      @Override
      public void onSuccess(UserProxy response) {
        setText(response.getNickname());
        show();
      }
    });
    request.fire();
    
    ChannelServiceRequest channelServiceRequest = requestFactory.channelServiceRequest();
    channelServiceRequest.getChannel(channel.getChannelId()).to(new Receiver<ChannelProxy>() {
      @Override
      public void onSuccess(ChannelProxy channel) {
        setChannelProxy(channel);
      }
    });
    channelServiceRequest.fire();
  }
  
  private void sendMsg() {
    List<String> list = dataProvider.getList();
    list.add(inputTxt.getText());
    ChannelServiceRequest request = requestFactory.channelServiceRequest();
    System.out.println("channelProxy: " + channelProxy);
    request.sendMsgToChannel(channelProxy, inputTxt.getText()).to(new Receiver<String>() {
      @Override
      public void onSuccess(String response) {
        inputTxt.setText("");
      }
    });
    request.fire();
  }
  
  private void receiveMsg(String message) {
    AutoBean<ChannelProxy> bean = AutoBeanCodex.decode(factory, ChannelProxy.class, message);
    ChannelProxy channel = bean.as();
    if (channelProxy.getChannelId().equals(channel.getChannelId())) {
      List<String> list = dataProvider.getList();
      System.out.println(channel.getMessage());
      list.add(channel.getMessage());
    }
  }
}
